package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot_reggie.Service.DishFlavorService;
import com.boot_reggie.Service.impl.Dishserviceimpl;
import com.boot_reggie.Service.impl.SendMailServiceimpl;
import com.boot_reggie.Service.impl.SetmealDishServiceimpl;
import com.boot_reggie.Service.impl.SetmealServiceimpl;
import com.boot_reggie.common.CustomException;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.*;
import com.boot_reggie.filter.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dishes")
public class DisController {
    @Autowired
    private Dishserviceimpl dishserviceimpl;
    @Autowired
    private DishFlavorService service;
    @Autowired
    private SetmealServiceimpl setmealServiceimpl;
    @Autowired
    private SendMailServiceimpl sendMailServiceimpl;
    @Autowired
    private SetmealDishServiceimpl setmealDishServiceimpl;
    @PostMapping
    public Result<String> save(@RequestBody DishDto dishDto){
        System.out.println(dishDto.getImage());
        System.out.println(dishDto.getFlavors());
        System.out.println(dishDto.getCategoryId());
        System.out.println(dishDto.getName());
        System.out.println(dishDto.getPrice());
        System.out.println(dishDto.getDescription());
        System.out.println(dishDto.getFlavors());
        try {
            dishserviceimpl.saveWithFlavor(dishDto);

        }catch (Exception ex){
            return Result.error("数据添加失败");
        }
        return Result.success("添加成功");
    }
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null,Dish::getName,name);
        queryWrapper.eq(name != null,Dish::getIsDeleted,0);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishserviceimpl.page(pageInfo,queryWrapper);
        return Result.success(pageInfo,"获取成功");
    }
    @GetMapping("byId")
    public Result<DishDto> getID(Dish dish){
        DishDto dishDto = new DishDto();
        try{
            dishDto = dishserviceimpl.getid(dish.getId());
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return Result.success(dishDto,"数据获取成功");
    }
    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto){
//        System.out.println(dishDto.getImage());
//        System.out.println(dishDto.getFlavors());
//        System.out.println(dishDto.getCategoryId());
//        System.out.println(dishDto.getName());
//        System.out.println(dishDto.getPrice());
//        System.out.println(dishDto.getDescription());
//        System.out.println(dishDto.getFlavors());
        try {
//            dishserviceimpl.updateWithFlavor(dishDto);
            dishserviceimpl.updateWithFlavor(dishDto);

        }catch (Exception ex){
            return Result.error("数据添加失败");
        }
        return Result.success("添加成功");
    }
    @DeleteMapping("del")
    public Result<String> delete(@RequestBody Dish dish){
        System.out.println(dish);
//        先判断菜品是否存在于套餐里
        LambdaQueryWrapper<SetmealDish> lwq = new LambdaQueryWrapper<>();
        lwq.eq(SetmealDish::getDishId,dish.getId());
        int count = setmealDishServiceimpl.count(lwq);
        if(count > 0){
            throw new CustomException("菜品绑定有套餐，删除失败");
        }
//        如果没有则放行



        //        将is_delete 改为1表示删除
        boolean data = dishserviceimpl.updateById(dish);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        DishFlavor flavor = new DishFlavor();
        flavor.setIsDeleted(1);
        service.update(flavor,queryWrapper);
        if(data){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
    @PostMapping("saves")
    public Result<String> saves(@RequestBody List<Dish> dishList){
        dishList.stream().map((item)->{
            item.setIsDeleted(1);
            return item;
        }).collect(Collectors.toList());
        System.out.println(dishList);
        dishserviceimpl.deleLists(dishList);

        return Result.success("成功!");
    }
    @PostMapping("updates")
    public Result<String> updates(@RequestBody List<Dish> dishList){
        dishList.stream().map((item)->{
            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());
        dishserviceimpl.deleLists(dishList);
        return Result.success("成功");
    }
    @PostMapping("updatees")
    public Result<String> updatees(@RequestBody List<Dish> dishList){
        dishList.stream().map((item)->{
            item.setStatus(0);
            return item;
        }).collect(Collectors.toList());
        dishserviceimpl.deleLists(dishList);
        return Result.success("成功");
    }
//前台菜品展示
    @GetMapping("dish")
    public Result<List<CategoryDto>> dishGet(HttpSession session){
        System.out.println(BaseContext.getCurrentID());
        System.out.println(BaseContext.getThreadLocal1());
        List<CategoryDto> list = dishserviceimpl.getDishList();
        for (CategoryDto categoryDto : list) {
            if(categoryDto.getType() == 2){
//                获取分类名称在套餐表里查找
                LambdaQueryWrapper<Setmeal> lwq = new LambdaQueryWrapper<>();
//                查询id一样的套餐名
                lwq.eq(categoryDto.getId()!=null,Setmeal::getCategoryId,categoryDto.getId());
                lwq.eq(Setmeal::getStatus,1);
                List<Setmeal> setmeals = setmealServiceimpl.list(lwq);
//                创建一个套餐Dto表复制
                List<SetmealDto> setmealDtoList = new ArrayList<>();
                for (Setmeal setmeal : setmeals) {
                    SetmealDto setmealDto = new SetmealDto();
                    BeanUtils.copyProperties(setmeal,setmealDto);
                    setmealDtoList.add(setmealDto);
                };
//                获取每一个套餐包含的菜品
                for (SetmealDto setmealDto : setmealDtoList) {
                    LambdaQueryWrapper<SetmealDish> lwqdish = new LambdaQueryWrapper<>();
                    lwqdish.eq(SetmealDish::getSetmealId,setmealDto.getId());
                    List<SetmealDish> dishList = setmealDishServiceimpl.list(lwqdish);
                    setmealDto.setSetmealDishes(dishList);
                }
                /*将获取到的套餐信息注入*/
                categoryDto.setSetmealDtos(setmealDtoList);
//                for (Setmeal setmeal1 : setmeals) {
//                    LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                    setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal1.getId());
//                    List<SetmealDish> setmealDishList = setmealDishServiceimpl.list(setmealDishLambdaQueryWrapper);
//
//                };
//                categoryDto.setSetmealDtos(setmealDtoList);
//                System.out.println(setmealDtoList);
            }
        }
        return Result.success(list,"数据获取成功");
    }
}
